package com.khaled.protclass.server.common;

import java.util.ArrayList;
import java.util.Hashtable;

import com.khaled.protclass.model.util.UtilDirectory;

public class Configuration
{
	private static Configuration configuration;

	private Hashtable<String, String> htConfig;

	private String workingDirectory;

	private Configuration()
	{

	}

	public void loadConfiguration(String workingDirectory) throws Exception
	{
		this.workingDirectory = workingDirectory;
		String filePath = this.workingDirectory + "\\config.txt";

		htConfig = new Hashtable<String, String>();
		ArrayList<String> lines = UtilDirectory.readFileLines(filePath);

		for (String line : lines)
		{
			String[] split = line.split("=");
			this.htConfig.put(split[0].trim(), split[1].trim());
		}
	}

	public static Configuration getInstance()
	{
		if (configuration == null)
		{
			try
			{
				configuration = new Configuration();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		return configuration;
	}

	public String get(String key)
	{
		if (this.htConfig != null) { return this.htConfig.get(key); }

		return null;
	}

	public void set(String key, String value)
	{
		this.htConfig.put(key, value);
	}

	public String getWorkingDirectory()
	{
		return this.workingDirectory;
	}

	public void setWorkingDirectory(String dir)
	{
		this.workingDirectory = dir;
	}
}
